﻿<%@Page Language="C#" MasterPageFile="~/Views/Shared/_Layout.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

@{
	ViewBag.Title = "Feed";

    string detailUrl = ("~{0}/details/".FormatInvariant((string)Model.CurrentFeed.Url)).ResolveUri(ViewContext.HttpContext);
    
    string feedUrl = ("~{0}/v1/Packages/".FormatInvariant((string)Model.CurrentFeed.Url)).ResolveUri(ViewContext.HttpContext);
    string feedFilter;

    if (!Request.TryGetQueryStringValue<string>("filter", out feedFilter))
    {
        feedFilter = string.Empty; 
    }
    
    int pageSize = 10;
    int pageCount; 
    int pageIndex;

    if (Request.TryGetQueryStringValue<int>("pageCount", out pageCount))
    {
        pageCount = Math.Max(pageCount, 1);
    }
    else
    { 
        pageCount = NuGet.Enterprise.Server.Xml.XmlPackageReader.GetCount(feedUrl, feedFilter) / pageSize;
    }
    
    if (Request.TryGetQueryStringValue<int>("pageIndex", out pageIndex))
    {
        pageIndex = Math.Min(Math.Max(pageIndex, 0), pageCount - 1);
    }
    
    string pageUrl = "{0}/{1}".FormatInvariant(Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped), Request.Url.GetComponents(UriComponents.Path, UriFormat.Unescaped));
    string pageUrlPrev = "{0}?filter={1}&pageCount={2}&pageIndex={3}".FormatInvariant(pageUrl, feedFilter, pageCount, Math.Max(pageIndex - 1, 0));
    string pageUrlNext = "{0}?filter={1}&pageCount={2}&pageIndex={3}".FormatInvariant(pageUrl, feedFilter, pageCount, Math.Min(pageIndex + 1, pageCount - 1));
}
			
@section sidebar {

    @{Html.RenderPartial("_SidebarFeedSearch");}
    @{Html.RenderPartial("_SidebarFeedDetails");}
    @{Html.RenderPartial("_SidebarFeedNav");}
}

<h1>Feed - @Model.CurrentFeed.DisplayName</h1>

<div class="archive-pagination archive-pagination-top">
	<div class="left"><a href="@pageUrlPrev">« Previous page</a></div>
	<div class="right"><a href="@pageUrlNext">Next page »</a></div>
	<div class="clearer">&nbsp;</div>
</div>

@Html.RenderPackageFeed(feedUrl, feedFilter, pageIndex, pageSize, detailUrl);

<div class="archive-pagination archive-pagination-bottom">
	<div class="left"><a href="@pageUrlPrev">« Previous page</a></div>
	<div class="right"><a href="@pageUrlNext">Next page »</a></div>
	<div class="clearer">&nbsp;</div>
</div>

